package com.example.app2024.navigationButtom;

import androidx.lifecycle.ViewModelProvider;

import android.animation.ObjectAnimator;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

import com.example.app2024.R;

public class TwoFragment extends Fragment
{

      private TwoViewModel mViewModel;
      private ImageView imageView;

      public static TwoFragment newInstance()
      {
            return new TwoFragment();
      }

      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
      {
            View view = inflater.inflate(R.layout.fragment_two, container, false);
            imageView = view.findViewById(R.id.imageView);
            return  view;
      }

      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      {
            super.onActivityCreated(savedInstanceState);
            mViewModel = new ViewModelProvider(requireActivity()).get(TwoViewModel.class);
            imageView.setScaleX(mViewModel.scaleFactor);
            imageView.setScaleY(mViewModel.scaleFactor);
            // TODO: Use the ViewModel
            ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView, "scaleX",0, 0 , 0 , 0 );
            ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView, "scaleY",0, 0 , 0 , 0 );
            AnimationSet set;  //动画组, 这里没有使用
            objectAnimatorX.setDuration(500);
            objectAnimatorY.setDuration(500);

            imageView.setOnClickListener(v -> {
                  if (!objectAnimatorX.isRunning())
                  {
                        objectAnimatorX.setFloatValues(imageView.getScaleX() + 0.1f);
                        objectAnimatorY.setFloatValues(imageView.getScaleY() + 0.1f);
                        mViewModel.scaleFactor += 0.1;
                        objectAnimatorX.start();
                        objectAnimatorY.start();
                  }
            });

      }

}